"""
    参数化
    @pytest.mark.paramentrize()
"""
from api.login import LoginApi
from common.yaml_util import write_yaml
import pytest
import yaml
import config

"""
    登录验证码
    
"""
# # 发送请求
# response = requests.get("http://kdtx-test.itheima.net/api/captchaImage")

# # 查看响应
# print(response.status_code)
# print(response.headers)
# print(response.content)
# print(response.text)
# print(response.json())


def yaml_data():
    # 获取test_yaml.yaml
    with open(config.Base_Path + '/data/test_data.yaml', 'r', encoding='utf') as f:
        yaml_data = yaml.load(f, Loader=yaml.FullLoader)
    # test_data = []
    # for data in yaml_data:
    #     test_data.append((data.get('username'),
    #                       data.get('password'),
    #                       data.get('code'),
    #                       data.get('response_status'),
    #                       data.get('response_msg'),
    #                       data.get('response_code'),
    #                       ))
    # print(test_data)
    return yaml_data


class Test01Login:

    # # 前置处理
    # def setup_method(self):
    #     # 实例化接口对象
    #     print("开始")
    #     self.login_api = LoginApi()
    #
    # # 后置处理
    # def teardown_method(self):
    #     pass
    # 获取验证码

    def test01_verify_code(self):
        # 获取验证码
        response = LoginApi().get_verify_code()
        print(response.text)
        print("状态码:",response.status_code)
        print("uuid:",response.json().get("uuid"))
        # 将uuid保存到extract.yaml中
        write_yaml({"uuid":response.json().get("uuid")})
        # 断言
        assert 200 == response.status_code
        assert '成功' in response.text
        assert 200 == response.json().get("code")


    # 准备测试数据

    test_data = [
        # (username,password,code,response_status,response_msg,response_code)
        ('admin','HM_2023_test','2',200,'成功',200),
        ('admin', '123456', '2', 200, '错误', 500),
        ('admin','HM_2023_test','1',200,'错误',500),
        ('','HM_2023_test','2',200,'错误',500),
        ('jackChen', 'HM_2023_test', '2', 200, '错误', 500)
    ]

    @pytest.mark.parametrize("yaml_data",yaml_data())
    def test02_login(self,yaml_data):
        # 登录
        print(yaml_data)
        login_data = {
            "username":yaml_data.get('username'),
            "password":yaml_data.get('password'),
            "code":yaml_data.get('code'),
            "uuid":LoginApi().get_verify_code().json().get('uuid')
            }
        login_header = {"Content-Type": "application/json"}
        response = LoginApi().login(login_data=login_data, login_header=login_header)
        print(response.json())
        print("状态码:", response.status_code)
        print("token:",response.json().get("token"))
        # 将token保存到extract.yaml中
        write_yaml({"token":response.json().get("token")})
        assert yaml_data.get('response_status') == response.status_code
        assert yaml_data.get('response_msg') in response.text
        assert yaml_data.get('response_code') == response.json().get("code")


